package util;

import context.*;

import java.awt.Graphics2D;
import java.awt.geom.Point2D;

public class Linjal implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG

  private Point2D.Double p0;
  private double length;
  
  public Linjal(Point2D.Double p0,double length)
  { this.p0=p0;
    this.length=length;
  }
  
  public void paintKoter(Graphics2D g,double base,double[] dist,double scale)
  { if(TEGN_LINJALER)
    { double d10=0.10*scale;
      double heightL=length*scale;
      double xL=p0.x;
      double yL=p0.y;
      Util.drawLine(g,xL,yL-2*d10,xL,yL+heightL+2*d10);
      double prevDist=0;
      for(int i=0;i<dist.length;i++)
      { double dd=dist[i];
        Util.drawLine(g,xL-d10,yL+dd*scale,xL+d10,yL+dd*scale); // Tverrlinje etter 'dist[i]'
        double diff=dd-prevDist;
        if(diff>0.30 || i==(dist.length-1))
        {  
          if(diff>0.30) Util.label(g,""+(int)(diff*100+0.5),xL,yL+(prevDist+diff/2)*scale);
          double kote=base+length-prevDist;
          int dec=(int)(kote*10+0.5);
          int pre=dec/10;
          int pst=dec-pre*10;
//          Util.label(g,""+(int)(kote*100+0.5),xL+0.5*scale,yL+(prevDist)*scale);
          Util.label(g,"Kote "+pre+'.'+pst,xL+1.10*scale,yL+(prevDist)*scale);
        }
        prevDist=dd;
      }
    }    
  }
  
  public void paintVertikal(Graphics2D g,double[] dist,double scale)
  { if(TEGN_LINJALER)
    { double d10=0.10*scale;
      double heightL=length*scale;
      double xL=p0.x;
      double yL=p0.y;
      Util.drawLine(g,xL,yL-2*d10,xL,yL+heightL+2*d10);
      double prevDist=0;
      for(int i=0;i<dist.length;i++)
      { double dd=dist[i];
        Util.drawLine(g,xL-d10,yL+dd*scale,xL+d10,yL+dd*scale); // Tverrlinje etter 'dist[i]'
        double diff=dd-prevDist;
        if(diff>0.30)
        {  
          Util.label(g,""+(int)(diff*100+0.5),xL,yL+(prevDist+diff/2)*scale);
        }
        prevDist=dd;
      }
    }    
  }
  
  public void paintHorisontal(Graphics2D g,double[] dist,double scale)
  { if(TEGN_LINJALER)
  {
//    double d10=0.10*scale;
//    double heightL=length*scale;
//    double xL=p0.x;
//    double yL=p0.y;
    double d10=0.10*scale;
    double widthL=length*scale;
    double xL=p0.x;
    double yL=p0.y;
//    Util.drawLine(g,xL,yL-2*d10,xL,yL+heightL+2*d10);
    Util.drawLine(g,xL-2*d10,yL,xL+widthL+2*d10,yL);
    double prevDist=0;
    for(int i=0;i<dist.length;i++)
    { double dd=dist[i];
//      Util.drawLine(g,xL-d10,yL+dd*scale,xL+d10,yL+dd*scale); // Tverrlinje etter 'dist[i]'
      Util.drawLine(g,xL+dd*scale,yL-d10,xL+dd*scale,yL+d10); // Tverrlinje etter 'dist[i]'
      double diff=dd-prevDist;
      if(diff>0.30)
      {  
//        Util.label(g,""+(int)(diff*100+0.5),xL,yL+(prevDist+diff/2)*scale);
        Util.label(g,""+(int)(diff*100+0.5),xL+(prevDist+diff/2)*scale,yL);
      }
      prevDist=dd;
    }    
  }
  }
}
